home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0109_Using C And Pascal - Link.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  5KB  |  166 lines

  1. (*
  2. YK>1) I'm going to write a program in pascal that calls a function.
  3. YK>2) That function is going to be written in C.
  4. YK>3) Link them together to make one EXE file.
  5.  
  6. YK>Is there anyway to do this or am I just dreaming? <g>  Thanks for
  7. YK>any insight in this.
  8.  
  9. Yes, it is possible.  You will need to compile object code modules with
  10. your Pascal and C compilers, and then link them with a linker program.
  11. Unusually, for a programming tool, the program you use for linking usually
  12. has the obvious name of LINK.EXE (as compared to such things as "grep",
  13. "awk", "yacc" or "bison").
  14.  
  15. The second edition of Turbo C++ includes a set of example files for just
  16. this situation.
  17.  
  18. First, a fragment of the C code called by the Pascal program.
  19.  
  20.  
  21. typedef unsigned int word;
  22. typedef unsigned char byte;
  23. typedef unsigned long longword;
  24.  
  25. extern void setcolor(byte newcolor);  /* procedure defined in
  26.                                          Turbo Pascal program */
  27. extern word factor;    /* variable declared in Turbo Pascal program */
  28.  
  29. word sqr(int i)
  30. {
  31.   setcolor(1);
  32.   return(i * i);
  33. } /* sqr */
  34.  
  35. word multbyfactor(word w)
  36. {
  37.   setcolor(9);        /* note that this function accesses the Turbo Pascal */
  38.   return(w * factor); /* declared variable factor */
  39. } /* multbyfactor */
  40.  
  41. ----8<---------
  42.  
  43. The command line compiler uses the following .CFG file
  44.  
  45. ---8<---------
  46.  
  47. -wrvl
  48. -p
  49. -k-
  50. -r-
  51. -u-
  52. -zCCODE
  53. -zP
  54. -zA
  55. -zRCONST
  56. -zS
  57. -zT
  58. -zDDATA
  59. -zG
  60. -zB
  61.  
  62. ---8<------------
  63.  
  64. Finally, the Pascal code
  65.  
  66. *)
  67. program CPASDEMO;
  68. (*
  69.   This program demonstrates how to interface Turbo Pascal and Turbo C++.
  70.   Turbo C++ is used to generate an .OBJ file (CPASDEMO.OBJ). Then
  71.   this .OBJ is linked into this Turbo Pascal program using the {$L}
  72.   compiler directive.
  73.  
  74.   NOTES:
  75.     1. Data declared in the Turbo C++ module cannot be accessed from
  76.        the Turbo Pascal program. Shared data must be declared in
  77.        Pascal.
  78.  
  79.     2. If the C functions are only used in the implementation section
  80.        of a unit, declare them NEAR.  If they are declared in the
  81.        interface section of a unit, declare them FAR.  Always compile
  82.        the Turbo C++ modules using the small memory model.
  83.  
  84.     3. Turbo C++ runtime library routines cannot be used because their
  85.        modules do not have the correct segment names.  However, if you
  86.        have the Turbo C++ runtime library source (available from
  87.        Borland), you can use individual library modules by recompiling
  88.        them using Pascal conventions.  If you do recompile them, make
  89.        sure that you include prototypes in your C module for all C
  90.        library functions that you use.
  91.  
  92.     4. Some of the code that Turbo C++ generates are calls to internal
  93.        routines. These cannot be used without recompiling the relevant
  94.        parts of the Turbo C++ runtime library source code.
  95.  
  96.   In order to run this demonstration program you will need the following
  97.   files:
  98.  
  99.     TCC.EXE and CTOPAS.CFG or
  100.     TC.EXE and CTOPAS.TC
  101.  
  102.   To run the demonstration program CPASDEMO.EXE do the following:
  103.  
  104.   1. First create a CPASDEMO.OBJ file compatible with Turbo Pascal 4.0
  105.      or later using Turbo C++.
  106.  
  107.     a) If you are using the Turbo C++ integrated environment (TC.EXE)
  108.        then at the DOS prompt execute:
  109.  
  110.        TC CTOPAS.PRJ
  111.  
  112.        then create the .OBJ file by pressing ALT-F9.
  113.  
  114.     b) If you are using the Turbo C++ command line version (TCC.EXE)
  115.        then at the DOS prompt execute:
  116.  
  117.        TCC +CTOPAS.CFG CPASDEMO.C
  118.  
  119.        Note: Use the same configuration file (CTOPAS.CFG or CTOPAS.PRJ)
  120.              when you create your own Turbo C++ modules for use with
  121.              Turbo Pascal
  122.  
  123.   2. Compile and execute the Turbo Pascal program CPASDEMO.PAS
  124.  
  125.   This simple program calls each of the functions defined in the Turbo C++
  126.   module. Each of the Turbo C++ functions changes the current display color
  127.   by calling the Turbo Pascal procedure SetColor. }
  128. *)
  129.  
  130. uses Crt;
  131.  
  132. var
  133.   Factor : Word;
  134.  
  135. {$F+}  { Force Far Calls for calling to and from Turbo C }
  136.  
  137. {$L CPASDEMO.OBJ}  { link in the Turbo C++-generated .OBJ module }
  138.  
  139. function Sqr(I : Integer) : Word; external;
  140. { Change the text color and return the square of I }
  141.  
  142. function MultByFactor(W : Word) : Word; external;
  143. { Change the text color and return W * Factor - note Turbo C++'s access of }
  144. { Turbo Pascal's global variable.                                        }
  145.  
  146. procedure SetColor(NewColor : Byte); { A procedure that changes the current }
  147. begin                                { display color by changing the CRT    }
  148.   TextAttr := NewColor;              { variable TextAttr                    }
  149. end; { SetColor }
  150.  
  151. begin
  152.   Writeln(Sqr(10));                  { Call each of the functions defined   }
  153.                                      { passing it the appropriate info.}
  154.  
  155.   Factor :=100;
  156.   Writeln(MultbyFactor(10));
  157.   SetColor(LightGray);
  158.  
  159. end.
  160. {
  161. ------8<----------
  162.  
  163. To save space, I've edited a lot of the functions out of both source
  164. files.  I hope this works.  I don't have a DOS Pascal compiler :(
  165. }
  166.